﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.UI;

namespace YJKAxisAndEdit
{
	public partial class DlgGridPartNum : System.Windows.Forms.Form
	{
		public DlgGridPartNum(ExternalCommandData cmdData)
		{
			this.InitializeComponent();
		}

		public string ValidGridName
		{
			get
			{
				return this.m_PartNumber;
			}
			set
			{
				this.m_PartNumber = value;
			}
		}

		public string PartNum
		{
			get
			{
				return this.tbPartNum.Text;
			}
			set
			{
				this.tbPartNum.Text = value;
			}
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void KeyEsc()
		{
			byte bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			DlgGridPartNum.SendMessage(CmdGridPartNum._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			DlgGridPartNum.keybd_event(bVk, 0, dwFlags, 0);
			DlgGridPartNum.keybd_event(bVk, 0, dwFlags2, 0);
		}

		private void DlgGridPartNum_MouseLeave(object sender, EventArgs e)
		{
			if (!this.IsValidatingOfNumber())
			{
				return;
			}
			this.m_PartNumber = this.tbPartNum.Text;
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				int wMsg = 7;
				DlgGridPartNum.SendMessage(CmdGridPartNum._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		public bool IsValidatingOfNumber()
		{
			string text = "";
			if (!this.IsValidatingOfNumber(this.tbPartNum.Text, ref text))
			{
				MessageBox.Show(text, "盈建科提示");
				this.tbPartNum.Text = this.m_PartNumber;
				return false;
			}
			return true;
		}

		private bool IsValidatingOfNumber(string str, ref string message)
		{
			string containsPrefix = AxisNameManager.GetContainsPrefix(str);
			if (containsPrefix.CompareTo(this.tbPartNum.Text) != 0)
			{
				List<string> connectorList = AxisNameManager.GetConnectorList();
				string text = "";
				foreach (string str2 in connectorList)
				{
					text = text + "'" + str2 + "' ";
				}
				message = "格式不合法，格式应为'前缀名'加'连接符'，'连接符'包括：" + text;
				return false;
			}
			return true;
		}

		private void DlgGridPartNum_Load(object sender, EventArgs e)
		{
			this.ReadData();
		}

		private void DlgGridPartNum_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveData();
		}

		private void DlgGridPartNum_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.KeyEsc();
		}

		private void SaveData()
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "GridPartNumForm.dat";
			FileStream fileStream = new FileStream(path, FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.WriteLine(this.m_PartNumber);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void ReadData()
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "GridPartNumForm.dat";
			if (!File.Exists(path))
			{
				base.StartPosition = FormStartPosition.CenterScreen;
				this.m_PartNumber = "A-";
				this.tbPartNum.Text = this.m_PartNumber;
				return;
			}
			base.StartPosition = FormStartPosition.Manual;
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			try
			{
				int x = Convert.ToInt32(streamReader.ReadLine());
				int y = Convert.ToInt32(streamReader.ReadLine());
				base.Location = new Point(x, y);
				string text = streamReader.ReadLine();
				string text2 = "";
				if (!this.IsValidatingOfNumber(text, ref text2))
				{
					this.m_PartNumber = "A-";
				}
				else
				{
					this.m_PartNumber = text;
				}
				this.tbPartNum.Text = this.m_PartNumber;
			}
			catch
			{
			}
			finally
			{
				streamReader.Close();
				fileStream.Close();
			}
		}

		private string m_PartNumber = "";
	}
}
